package com.ming.closeto.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ming.closeto.R;
import com.ming.closeto.base.BaseActivity;
import com.ming.closeto.entity.Constant;
import com.ming.closeto.model.MovieMoreModel;
import com.ming.closeto.utils.GlideUtils;
import com.ming.closeto.utils.LogUtils;
import com.ming.closeto.utils.OkHttpUtils;
import com.ming.closeto.view.AutoViewPager;

import java.io.IOException;

public class MovieMoreActivity extends BaseActivity {

    private ImageView iv_poster;
    private TextView tv_writers;
    private TextView tv_plot_simple;

    private AutoViewPager mAutoViewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_more);

        initView();
    }

    private void initView() {
        // 接收数据
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String title = intent.getStringExtra("title");
        if (!TextUtils.isEmpty(title)) {
            getSupportActionBar().setTitle(title);
        }

        iv_poster = (ImageView) findViewById(R.id.iv_poster);
        tv_writers = (TextView) findViewById(R.id.tv_writers);
        tv_plot_simple = (TextView) findViewById(R.id.tv_plot_simple);

        mAutoViewPager = findViewById(R.id.mAutoViewPager);
        mAutoViewPager.setAutoTime(2000);
        mAutoViewPager.setStartAuto(true);

        initData(id);

    }

    private void initData(String id) {
        String url = Constant.MOVIE_DETAIL + id;
        OkHttpUtils.get(url, new OkHttpUtils.OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {
                LogUtils.i(e.toString());
            }

            @Override
            public void onFailure(String msg) {
                LogUtils.i(msg);
            }

            @Override
            public void onSuccessful(String json) {
                if (!TextUtils.isEmpty(json)) {
                    LogUtils.i("电影详情:" + json);
                    loadMovieMoreData(json);
                }
            }
        });
    }

    // 解析数据
    private void loadMovieMoreData(String json) {
        MovieMoreModel model = new Gson().fromJson(json, MovieMoreModel.class);

        GlideUtils.loadImageviewCenterCrop(this, model.getResult().getPoster(), iv_poster);
        tv_plot_simple.setText(model.getResult().getPlot_simple());
        tv_writers.setText("主演：" + model.getResult().getWriters());

        for (int i=0; i < 4; i++) {
            View view = View.inflate(this, R.layout.layout_auto_view_pager_item, null);
            ImageView iv = view.findViewById(R.id.iv_bg);
            GlideUtils.loadImageviewCenterCrop(this, model.getResult().getPoster(), iv);
            mAutoViewPager.setView(view);
        }
    }
}
